//
//  XLogBridge.h
//  ACFrameworkSwift
//
//  Created by fisherman on 2021/9/2.
//

#import <Foundation/Foundation.h>
#import <mars/xlog/xloggerbase.h>

@interface XLogBridge : NSObject

///  配置项
/// @param logPath 日志路径
/// @param pubkey 加密的公钥
/// @param maxSize 日志文件最大 size
/// @param nameprefix 日志文件的前缀，默认是 `_20210902.xlog`
+ (void)configWithLogPath: (NSString *)logPath pubkey: (NSString *)pubkey maxSize: (uint64_t)maxSize nameprefix: (NSString *)nameprefix;

/// 完整日志
/// @param logLevel 日志级别
/// @param tag 标签，用于分类，也可以传入模块名
/// @param fileName 日志所在的文件
/// @param lineNumber 日志所在的行
/// @param funcName 日志所在的方法
/// @param message 定义的日志信息
+ (void)logWithLevel: (TLogLevel)logLevel tag: (const char*)tag fileName:(const char*)fileName lineNumber: (int)lineNumber funcName: (const char*)funcName message: (NSString *)message;

+ (void)logWithLevel:(TLogLevel)logLevel tag:(const char*)tag fileName:(const char*)fileName lineNumber:(int)lineNumber funcName:(const char*)funcName format:(NSString *)format, ...;

/// 根据设置的最低 log 级别来判断是否能写入
/// @param level 日志级别
+ (BOOL)shouldLog: (TLogLevel)level;

///  程序终止前的关闭
+ (void)close;

@end

#define LogInternal(level, tag, file, line, func, prefix, format, ...) \
do { \
    if ([XLogBridge shouldLog:level]) { \
        NSString *aMessage = [NSString stringWithFormat:@"%@%@", prefix, [NSString stringWithFormat:format, ##__VA_ARGS__, nil]]; \
        [XLogBridge logWithLevel:level tag:tag fileName:file lineNumber:line funcName:func message:aMessage]; \
    } \
} while(0)

#define __FILENAME__ (strrchr(__FILE__,'/')+1)

#define LOG_ERROR(tag, format, ...) LogInternal(kLevelError, tag, __FILENAME__, __LINE__, __FUNCTION__, @"Error:", format, ##__VA_ARGS__)
#define LOG_WARNING(tag, format, ...) LogInternal(kLevelWarn, tag, __FILENAME__, __LINE__, __FUNCTION__, @"Warning:", format, ##__VA_ARGS__)
#define LOG_INFO(tag, format, ...) LogInternal(kLevelInfo, tag, __FILENAME__, __LINE__, __FUNCTION__, @"Info:", format, ##__VA_ARGS__)
#define LOG_DEBUG(tag, format, ...) LogInternal(kLevelDebug, tag, __FILENAME__, __LINE__, __FUNCTION__, @"Debug:", format, ##__VA_ARGS__)
